﻿Imports Microsoft.VisualBasic
Imports System.Data

Namespace HensonOnline
    Public Class Messages

        Private _MessageId As Integer
        Private _FromId As Integer
        Private _ToId As Integer
        Private _Subject As String
        Private _Message As String
        Private _MessageDate As Date
        Private _Read As Boolean
        Private _ThreadId As Integer

        Public Property MessageId() As Integer
            Get
                Return _MessageId
            End Get
            Set(ByVal value As Integer)
                _MessageId = value
            End Set
        End Property

        Public Property FromId() As Integer
            Get
                Return _FromId
            End Get
            Set(ByVal value As Integer)
                _FromId = value
            End Set
        End Property

        Public Property ToId() As Integer
            Get
                Return _ToId
            End Get
            Set(ByVal value As Integer)
                _ToId = value
            End Set
        End Property

        Public Property Subject() As String
            Get
                Return _Subject
            End Get
            Set(ByVal value As String)
                _Subject = value
            End Set
        End Property

        Public Property Message() As String
            Get
                Return _Message
            End Get
            Set(ByVal value As String)
                _Message = value
            End Set
        End Property

        Public Property MessageDate() As Date
            Get
                Return _MessageDate
            End Get
            Set(ByVal value As Date)
                _MessageDate = value
            End Set
        End Property

        Public Property Read() As Boolean
            Get
                Return _Read
            End Get
            Set(ByVal value As Boolean)
                _Read = value
            End Set
        End Property

        Public Property ThreadId() As Integer
            Get
                Return _ThreadId
            End Get
            Set(ByVal value As Integer)
                _ThreadId = value
            End Set
        End Property

        Private Sub PopulateDefault()
            _MessageId = 0
            _FromId = 0
            _ToId = 0
            _Subject = ""
            _Message = ""
            _MessageDate = CDate("1/1/1900")
            _Read = False
            _ThreadId = 0
        End Sub

        Public Sub New()
            PopulateDefault()
        End Sub

        Public Function GetMessage(ByVal userId As Integer, ByVal messageId As Integer) As ArrayList
            Dim sql As String = "SELECT Messages.MessageID, Messages.FromID, Messages.ToID, Messages.Subject, Messages.Message, Messages.Date, Messages.[Read], " _
                              & "Messages.ThreadId, Users.FName, Users.LName FROM Messages INNER JOIN Users ON Messages.FromID = Users.UserID " _
                              & "WHERE  (Messages.ToID = @UserID) AND (Messages.MessageID = @MessageID)"
            Dim type As CommandType = CommandType.Text
            Dim parameters As List(Of System.Data.SqlClient.SqlParameter) = New List(Of System.Data.SqlClient.SqlParameter)
            parameters.Add(New System.Data.SqlClient.SqlParameter("@UserID", userId))
            parameters.Add(New System.Data.SqlClient.SqlParameter("@MessageID", messageId))
            Dim data As New DataAccess

            Dim propertyList As ArrayList = data.SelectSingle(sql, type, parameters.ToArray, 8)

            _MessageId = propertyList(0)
            _FromId = propertyList(1)
            _ToId = propertyList(2)
            _Subject = propertyList(3)
            _Message = propertyList(4)
            _MessageDate = propertyList(5)
            _Read = propertyList(6)
            _ThreadId = propertyList(7)

            Return propertyList
        End Function


    End Class
End Namespace
